package com.example.demo6.ui.detail;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.demo6.R;
import com.example.demo6.SQLite.LostGoods;
import com.example.demo6.SQLite.MyDatabase;
import com.example.demo6.SQLite.PickupGoods;

public class pickupdetailFragment extends Fragment {

    private TextView ShowPickupName, ShowPickup, ShowPickupPlace, ShowPickupTime, ShowPickupPhone, ShowPickupDetail;
    private Button PickupDetailReturn;

    PickupGoods pickupGoods;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pickupdetail, container, false);

        // 初始化NavController
        ShowPickupName = view.findViewById(R.id.ShowPickupName);
        ShowPickup = view.findViewById(R.id.ShowPickup);
        ShowPickupPlace = view.findViewById(R.id.ShowPickupPlace);
        ShowPickupTime = view.findViewById(R.id.ShowPickupTime);
        ShowPickupPhone = view.findViewById(R.id.ShowPickupPhone);
        ShowPickupDetail = view.findViewById(R.id.ShowPickupDetail);
        // 从Bundle中获取goodsId
        int pickupId = getArguments() != null ? getArguments().getInt("PickupId") : -1;
        if (pickupId != -1) {
            new SelectPickupGoodsTask(pickupId).execute();
        }

        // 设置返回按钮的点击监听器
        Button LostDetailReturn = view.findViewById(R.id.PickupDetailReturn);
        LostDetailReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController= Navigation.findNavController((Activity) getContext(),R.id.nav_host_fragment_activity_main);
                navController.popBackStack();
            }
        });

        return view;
    }

    @SuppressLint("StaticFieldLeak")
    private class SelectPickupGoodsTask extends AsyncTask<Void, Void, Void>
    {
        int pickupId;
        public SelectPickupGoodsTask(int pickupId)
        {
            this.pickupId=pickupId ;
        }
        //不能在这里更新UI,否则有异常
        @Override
        protected Void doInBackground(Void... arg0)
        {
            pickupGoods= MyDatabase.getInstance(getContext()).pickupGoodsDao().getPickupGoodsByPickupId(pickupId);
            return null;
        }
        //onPostExecute用于doInBackground执行完后，可以更新界面UI。
        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            // 绑定数据到视图
            bindDataToView(pickupGoods);

        }
    }

    private void bindDataToView(PickupGoods pickupGoods) {
        ShowPickupName.setText(pickupGoods.pickupGoods);
        ShowPickup.setText(pickupGoods.pickup);
        ShowPickupPlace.setText(pickupGoods.pickupPlace);
        ShowPickupTime.setText(pickupGoods.pickupTime);
        ShowPickupPhone.setText(pickupGoods.pickupPhone);
        ShowPickupDetail.setText(pickupGoods.pickupDetail);
    }
}